<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of smooth2a</title>
  <meta name="keywords" content="smooth2a">
  <meta name="description" content="Smooths 2D array data.  Ignores NaN's.">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2005 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
  <script type="text/javascript">
    if (top.frames.length == 0) { top.location = "../../index.html"; };
  </script>
</head>
<body>
<a name="_top"></a>
<!-- ../menu.html VMT_4.0_dev --><!-- menu.html utils -->
<h1>smooth2a
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>Smooths 2D array data.  Ignores NaN's.</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>function matrixOut = smooth2a(matrixIn,Nr,Nc) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment"> Smooths 2D array data.  Ignores NaN's.

function matrixOut = smooth2a(matrixIn,Nr,Nc)
 
 This function smooths the data in matrixIn using a mean filter over a
 rectangle of size (2*Nr+1)-by-(2*Nc+1).  Basically, you end up replacing
 element &quot;i&quot; by the mean of the rectange centered on &quot;i&quot;.  Any NaN
 elements are ignored in the averaging.  If element &quot;i&quot; is a NaN, then it
 will be preserved as NaN in the output.  At the edges of the matrix,
 where you cannot build a full rectangle, as much of the rectangle that
 fits on your matrix is used (similar to the default on Matlab's builtin
 function &quot;smooth&quot;).
 
 &quot;matrixIn&quot;: original matrix
 &quot;Nr&quot;: number of points used to smooth rows
 &quot;Nc&quot;: number of points to smooth columns.  If not specified, Nc = Nr.
 
 &quot;matrixOut&quot;: smoothed version of original matrix
 
 
     Written by Greg Reeves, March 2009.
     Division of Biology
     Caltech
 
     Inspired by &quot;smooth2&quot;, written by Kelly Hilands, October 2004
     Applied Research Laboratory
     Penn State University
 
     Developed from code written by Olof Liungman, 1997
     Dept. of Oceanography, Earth Sciences Centre
     G�teborg University, Sweden
     E-mail: olof.liungman@oce.gu.se</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
</ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
</ul>
<!-- crossreference -->



<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function matrixOut = smooth2a(matrixIn,Nr,Nc)</a>
0002 <span class="comment">% Smooths 2D array data.  Ignores NaN's.</span>
0003 <span class="comment">%</span>
0004 <span class="comment">%function matrixOut = smooth2a(matrixIn,Nr,Nc)</span>
0005 <span class="comment">%</span>
0006 <span class="comment">% This function smooths the data in matrixIn using a mean filter over a</span>
0007 <span class="comment">% rectangle of size (2*Nr+1)-by-(2*Nc+1).  Basically, you end up replacing</span>
0008 <span class="comment">% element &quot;i&quot; by the mean of the rectange centered on &quot;i&quot;.  Any NaN</span>
0009 <span class="comment">% elements are ignored in the averaging.  If element &quot;i&quot; is a NaN, then it</span>
0010 <span class="comment">% will be preserved as NaN in the output.  At the edges of the matrix,</span>
0011 <span class="comment">% where you cannot build a full rectangle, as much of the rectangle that</span>
0012 <span class="comment">% fits on your matrix is used (similar to the default on Matlab's builtin</span>
0013 <span class="comment">% function &quot;smooth&quot;).</span>
0014 <span class="comment">%</span>
0015 <span class="comment">% &quot;matrixIn&quot;: original matrix</span>
0016 <span class="comment">% &quot;Nr&quot;: number of points used to smooth rows</span>
0017 <span class="comment">% &quot;Nc&quot;: number of points to smooth columns.  If not specified, Nc = Nr.</span>
0018 <span class="comment">%</span>
0019 <span class="comment">% &quot;matrixOut&quot;: smoothed version of original matrix</span>
0020 <span class="comment">%</span>
0021 <span class="comment">%</span>
0022 <span class="comment">%     Written by Greg Reeves, March 2009.</span>
0023 <span class="comment">%     Division of Biology</span>
0024 <span class="comment">%     Caltech</span>
0025 <span class="comment">%</span>
0026 <span class="comment">%     Inspired by &quot;smooth2&quot;, written by Kelly Hilands, October 2004</span>
0027 <span class="comment">%     Applied Research Laboratory</span>
0028 <span class="comment">%     Penn State University</span>
0029 <span class="comment">%</span>
0030 <span class="comment">%     Developed from code written by Olof Liungman, 1997</span>
0031 <span class="comment">%     Dept. of Oceanography, Earth Sciences Centre</span>
0032 <span class="comment">%     G�teborg University, Sweden</span>
0033 <span class="comment">%     E-mail: olof.liungman@oce.gu.se</span>
0034 
0035 <span class="comment">%</span>
0036 <span class="comment">% Initial error statements and definitions</span>
0037 <span class="comment">%</span>
0038 <span class="keyword">if</span> nargin &lt; 2, error(<span class="string">'Not enough input arguments!'</span>), <span class="keyword">end</span>
0039 
0040 N(1) = Nr; 
0041 <span class="keyword">if</span> nargin &lt; 3, N(2) = N(1); <span class="keyword">else</span> N(2) = Nc; <span class="keyword">end</span>
0042 
0043 <span class="keyword">if</span> length(N(1)) ~= 1, error(<span class="string">'Nr must be a scalar!'</span>), <span class="keyword">end</span>
0044 <span class="keyword">if</span> length(N(2)) ~= 1, error(<span class="string">'Nc must be a scalar!'</span>), <span class="keyword">end</span>
0045 
0046 <span class="comment">%</span>
0047 <span class="comment">% Building matrices that will compute running sums.  The left-matrix, eL,</span>
0048 <span class="comment">% smooths along the rows.  The right-matrix, eR, smooths along the</span>
0049 <span class="comment">% columns.  You end up replacing element &quot;i&quot; by the mean of a (2*Nr+1)-by-</span>
0050 <span class="comment">% (2*Nc+1) rectangle centered on element &quot;i&quot;.</span>
0051 <span class="comment">%</span>
0052 [row,col] = size(matrixIn);
0053 eL = spdiags(ones(row,2*N(1)+1),(-N(1):N(1)),row,row);
0054 eR = spdiags(ones(col,2*N(2)+1),(-N(2):N(2)),col,col);
0055 
0056 <span class="comment">%</span>
0057 <span class="comment">% Setting all &quot;NaN&quot; elements of &quot;matrixIn&quot; to zero so that these will not</span>
0058 <span class="comment">% affect the summation.  (If this isn't done, any sum that includes a NaN</span>
0059 <span class="comment">% will also become NaN.)</span>
0060 <span class="comment">%</span>
0061 A = isnan(matrixIn);
0062 matrixIn(A) = 0;
0063 
0064 <span class="comment">%</span>
0065 <span class="comment">% For each element, we have to count how many non-NaN elements went into</span>
0066 <span class="comment">% the sums.  This is so we can divide by that number to get a mean.  We use</span>
0067 <span class="comment">% the same matrices to do this (ie, &quot;eL&quot; and &quot;eR&quot;).</span>
0068 <span class="comment">%</span>
0069 nrmlize = eL*(~A)*eR;
0070 nrmlize(A) = NaN;
0071 
0072 <span class="comment">%</span>
0073 <span class="comment">% Actually taking the mean.</span>
0074 <span class="comment">%</span>
0075 matrixOut = eL*matrixIn*eR;
0076 matrixOut = matrixOut./nrmlize;
0077 
0078 
0079 
0080 
0081 
0082 
0083 
0084 
0085 
0086 
0087 
0088</pre></div>
<hr><address>Generated on Thu 21-Mar-2013 09:32:01 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" target="_parent">m2html</a></strong> &copy; 2005</address>
</body>
</html>